const fs = require('fs');
const path = require('path');

// 读取Home.vue文件
const filePath = path.join(__dirname, 'homework-master', 'chat-ai-system-backup', 'frontend', 'src', 'views', 'Home.vue');
const content = fs.readFileSync(filePath, 'utf8');

// 提取script标签内容
const scriptMatch = content.match(/<script>([\s\S]*?)<\/script>/);
if (!scriptMatch) {
  console.error('找不到script标签');
  process.exit(1);
}

const scriptContent = scriptMatch[1];

// 写入临时JS文件以便检查
const tempJsPath = path.join(__dirname, 'temp-script.js');
// 移除import语句，替换为空对象
const cleanScript = scriptContent
  .replace(/import.*?from.*?['"].*?['"];/g, '')
  .replace(/export default {/g, 'const component = {');

fs.writeFileSync(tempJsPath, cleanScript);

// 尝试解析
console.log('正在检查语法...');
try {
  require('./temp-script.js');
  console.log('语法检查通过！');
} catch (e) {
  console.error('语法错误:', e.message);
  // 尝试获取错误位置
  const stackLines = e.stack.split('\n');
  for (const line of stackLines) {
    if (line.includes('temp-script.js:')) {
      const lineMatch = line.match(/temp-script\.js:(\d+):(\d+)/);
      if (lineMatch) {
        console.log(`错误位置: 第${lineMatch[1]}行, 第${lineMatch[2]}列`);
        // 显示错误行
        const lines = cleanScript.split('\n');
        const errorLine = parseInt(lineMatch[1]) - 1;
        if (errorLine >= 0 && errorLine < lines.length) {
          console.log('错误行内容:', lines[errorLine]);
        }
      }
      break;
    }
  }
}